package vy.org.app.common;

import java.util.Locale;

import javax.servlet.http.HttpServletRequest;

public class ActionCtx {
	
	private UserCtx user = null;
	private String lang = AppConstants.Languages.ENGLISH;
	
	private static ThreadLocal<ActionCtx> actionCtx = new ThreadLocal<ActionCtx>() {
        protected synchronized ActionCtx initialValue() {
        	return new ActionCtx();
        }
    };
 
    public static ActionCtx ctx(HttpServletRequest req) throws SessionExpiredException, Exception {
    	ActionCtx aCtx = actionCtx.get();
    	aCtx.initUserCtx(req);
    	return aCtx;
    }
    
    public static ActionCtx ctx() {
    	return actionCtx.get();
    }
    
    private void initUserCtx(HttpServletRequest req) throws SessionExpiredException, Exception {
    	user = new UserCtx(req);
    }
    
    public UserCtx getUser(){
    	return user;
    }

    public String getLang() {
		return lang;
	}

	public void setLang(String lang) {
		this.lang = lang;
	}
	
	public Locale getLocale(){
		if(AppConstants.Languages.ENGLISH.equals(lang)){
			return Locale.US;
		}else if(AppConstants.Languages.HINDI.equals(lang)
				|| AppConstants.Languages.KRUTI.equals(lang)){
			return new Locale("hi","IN");
		}
		return null;
	}
}

